package br.com.mundodaprata.store.data.gen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import br.com.mundodaprata.store.data.Repository;
import br.com.mundodaprata.store.data.RepositoryFactoryJDBC;
import br.com.mundodaprata.store.model.Product;

public class ProductGen implements DataGen{

   @Override
   public void gen() throws Exception {
      List<Product> products = new ArrayList<Product>();
      
      File file = new File("C:\\java\\products.txt");
      FileReader fr = new FileReader(file);
      BufferedReader reader = new BufferedReader(fr);
      String line;
      
      while ((line = reader.readLine()) != null) {
         String[] data = line.split(";");
         
         Product product = new Product();
         product.setName(data[0]);
         product.setModel(data[1]);
         product.setStock(Integer.parseInt(data[2]));
         product.setMinStock(Integer.parseInt(data[3]));
         product.setPrice(Double.parseDouble(data[4].replaceAll(",", ".")));
         
         products.add(product);
      }
      
      Repository<Product> jdbc = RepositoryFactoryJDBC.create(Product.class);
      jdbc.insert(products.toArray(new Product[products.size()]));
      jdbc.closeConnection();
   }
   
}